package net.lzzy.kvrdd;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

public class MapReduceBuKeyTest2 {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("MapToPair").setMaster("local");
        JavaSparkContext jsc = new JavaSparkContext(conf);
        JavaRDD<String> parallelize = jsc.textFile("src\\words.txt");

        JavaRDD<String> javaRDD = parallelize.flatMap(s-> Arrays.asList(s.split(" ")).iterator());

        JavaPairRDD<String, Integer> sijp = javaRDD.mapToPair(s -> new Tuple2<>(s.split(" ")[0], 1));

        JavaPairRDD<String, Integer> sijpr = sijp.reduceByKey((v1, v2)-> v1 + v2);

        sijpr.collect().forEach(sit2-> System.out.println(sit2));
    }
}
